<?

function handle_error($errno, $errstr) {
	header("Location: /holidays/server_slow.php");
	//var_dump($errstr);
	exit;
}

class holidays {


	var $inactive_tos = array();
	
	// Ski holiday airports VRN ?????
	var $ski_airports = "GRO,TLS,BCN,SZG,INN,BGY,VBS,TRN,SOF,PDV,LYS,GVA,OSL";
	var $ski_airports_array = array('GRO','TLS','BCN','SZG','INN','BGY','VBS','TRN','SOF','PDV','LYS','GVA','OSL');
	
	// Ski only TOs
	var $ski_only_tos = array( 
	//'Balkan Ski'
	'BSU', 
	//'Crystal Ski'
	'CRY',	
	//'Crystal Ski Euro'
	'CRE',	
	//'Crystal Ski'
	'YST',	
	//'First Choice Ski'
	'FCS',
	//'Inghams Ski'
	'ING',
	//'Neilson Ski'
	'JMN',  
	//'Neilson Ski'
	'NEI',
	//'Thomson Ski'
	'TSL',
	//'Thomson Ski'
	'THS'	);
	
	// Airport codes for checking wether the price is listed in sterling or euro
	var $north = array('Aberdeen' => 'ABZ',
	'Belfast City' => 'BHD',
	'Belfast International' => 'BFS',
	'Birmingham' => 'BHX',
	'Blackpool' => 'BLK',
	'Bournemouth' => 'BOH',
	'Bristol' => 'BRS',
	'Cardiff' => 'CWL',
	'City of Derry' => 'LDY',
	'Coventry' => 'CVT',
	'Durham Tees Valley' => 'MME',
	'Edinburgh' => 'EDI',
	'Exeter' => 'EXT',
	'Glasgow' => 'GLA',
	'Humberside' => 'HUY',
	'Inverness' => 'INV',
	'Leeds Bradford' => 'LBA',
	'Liverpool' => 'LPL',
	'London City Airport' => 'LCY',
	'London Gatwick' => 'LGW',
	'London Heathrow' => 'LHR',
	'London Luton' => 'LTN',
	'London Stansted' => 'STN',
	'Manchester' => 'MAN',
	'Newcastle' => 'NCL',
	'Norwich' => 'NWI',
	'Nottingham East Midlands' => 'EMA',
	'Prestwick' => 'PIK',
	'Southampton' => 'SOU');
	
	var $south = array('Cork' => 'ORK',
	'Dublin' => 'DUB',
	'Knock' => 'NOC',
	'Kerry' => 'KIR',
	'Galway' => 'GWY',
	'Shannon' => 'SNN');
	
	var $active_ni_tos = array(
							   'Falcon NI' => 'FS9',
							   'First Choice NI' => 'TFR',
							   'Thomson Villas' => 'OSL',
							   'Thomson' => 'TOP'
							   );

	

// FUNCTION // Get suppliment
function get_suppliment_price($tour_op_code){

	switch ($tour_op_code){
		case "JE2"; case "JT2"; case "LS"; 
		//	$suppliment = intval(10);
		break;
		case "STE"; case "STS";
			//$suppliment = intval(10);
			// to account for there flight suppliment..... back to 40 after october
		break;
		case "FAL"; case "FS9"; case "FE9"; case"FAA";
			//$suppliment = intval(90);
			// to account for the reduced comissions + suppliments
		break;
		case "FAN";
		// Falcon northern Ireland
			//$suppliment = intval(90);
			// to account for the reduced comissions + suppliments
		break;
		case "JWT"; case "JS9";  case "JE9";
			//$suppliment = intval(90);
			// to account for the reduced comissions + suppliments
		break;
		case "JWN";
		// JWT northern Ireland
			//$suppliment = intval(90);
			// to account for the reduced comissions + suppliments
		break;
		case "FIR"; case "FCS"; case "TFR";
		// First Joice 
			//$suppliment = intval(90);
			// to account for the reduced comissions + suppliments
		break;
		case "SWY";
			//$suppliment = intval(3);
			// to account for there flight suppliment
		break;
		case "TFL"; case "TFS";
			//$suppliment = intval(3);
			// to account for there flight suppliment
		break;
		case "SWI";
			//$suppliment = intval(0);
			// to account for there flight suppliment
		break;
		case "ABY";
			//$suppliment = intval(30);
			// to account for the?
		break;
		case "PAN"; case "PAI";
			//$suppliment = intval(0);
			// to account for the under occupancy
		break;
		case "CRY";
			//$suppliment = intval(30);
			// taxes
		break;
		case "CRI";
			//$suppliment = intval(30);
			// taxes
		break;
		case "SWH";
		case "SWM";
			//$suppliment = intval(80);
			// taxes
		break;
		case "SUN";
		case "SET";
			//$suppliment = intval(22);
			// taxes
		break;
		case "TCL";
			//$suppliment = intval(22);
			// taxes
		break;
	}
	

	return $suppliment;

}



// FUNCTION // Connect to SABS
function connection_sabs($request,$to_array=true){

	$service_port = "3000"; 
	$address = gethostbyname('api.ramesystravel.co.uk');
	
	set_time_limit(45);
	$old_error_handler = set_error_handler("handle_error", E_ERROR);
	// Refresh the page on timeouts
	// ini_set('error_prepend_string','<html><head><META http-equiv="refresh" content="0;URL=http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"].'"></head></html>');
	
	// Create a TCP/IP socket.
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die(socket_strerror($socket));
	if(!$socket) {
	   echo "socket_create() failed: reason:".socket_strerror($socket)."<BR><BR>";
	}
	
	// Bind to a set IP address 
	// if(!socket_bind($socket, '82.195.146.225')) echo "Coudn't bind to socket";
	
	// Connect to host with socket 
	$connection = socket_connect($socket, $address, $service_port) or die(socket_strerror($socket));
	
	if(!$connection) {
	   echo "socket_connect() failed.<br>Reason: (".$connection.") ".socket_strerror($connection)."<BR><BR>";
	}
	
	// SEND REQUEST
	socket_write($socket, $request, strlen($request));
	
	
	$buffer = "";
	while(strpos($buffer,'$$$')===false) {
		 $buffer = @socket_read($socket, 2048);
		 $response .= $buffer;
	}
	
	socket_close($socket);	

	restore_error_handler();
	
	//$time = time();
	//file_put_contents(SITE_ROOT_PATH."/_lib/logs/".$time."-response.txt", $response);
	
	if($to_array) $response = $this->sabs_response_to_array($response);
	
	return $response;

}





// FUNCTION // Tidy up SABS response into a array 
function sabs_response_to_array($message){

	$t = chr(9);
	$cr = chr(13);
	$lf = chr(10);
	
	// Tidy up
	$response_array = array();
	
	$response_array = explode($lf,$message);
	
	$count = count($response_array);
	
	$elements_array = array();
	
	for($i = 0;$i < $count; $i++){
		$elements_array[$i] = explode($t,$response_array[$i]);
	}
	
	return $elements_array;

}





// FUNCTION // Create search string for general grid search
function create_grq_query($params){


	if(empty($params['give_take'])) $params['give_take'] = '3';
	if(empty($params['departure_airport'])) $params['departure_airport'] = '*EIR'; //'*IRL';
	if(empty($params['pax_adult'])) $params['pax_adult'] = '2';
	if(empty($params['pax_child'])) $params['pax_child'] = '0';
	if(empty($params['pax_infant'])) $params['pax_infant'] = '0';
	
	$t = chr(9);
	$cr = chr(13);
	$lf = chr(10);
	
	switch ($params['nights']){
		case "2"; case "3"; case "4"; case "5";
			$min_nights = '2';
			$max_nights = '5';
		break;
		case "6"; case "7"; case "8";
			$min_nights = '6';
			$max_nights = '8';
		break;
		case "9"; case "10"; case "11"; case "12";
			$min_nights = '9';
			$max_nights = '12';
		break;
		case "13"; case "14"; case "15"; case "16"; case "17"; case "18"; case "19"; case "20"; case "21";
			$min_nights = '13';
			$max_nights = '28';
		break;
		default; 
			$min_nights = '';
			$max_nights = '';
		break;
	}
	
	/*if(in_array($params['departure_airport'], $this->north)){
		$preffered_tos = $this->active_ni_tos;
	}*/
	
	

	$request = "HDR".$t."CGO".$t."02".$t."000001".$t."".$t.MICROS_API_USER.$t.MICROS_API_PASS.$t."".$t."T".$t."Y".$t."N".$t."C".$cr.$lf;

	$request .= "GRQ".$t."000100".$t."0".$t."450".$t.$params['pax_adult'].$t.$params['pax_child'].$t.$params['pax_infant'].$t.$params['departure_date'].$t.$params['give_take'].$t.$min_nights.$t.$max_nights.$t.$params['destination_airport'].$t.$params['departure_airport'].$t."".$t."".$t.$preffered_tos[0].$t.$preffered_tos[1].$t.$preffered_tos[2].$t.$preffered_tos[3].$t."".$cr.$lf;

	$request .= "$$$".$cr.$lf;


	return $request;


}








// FUNCTION // Create search string for holiday only search
function create_cho_query($params){

	if(empty($params['departure_airport'])) $params['departure_airport'] = '*EIR'; //'*IRL';
	if(empty($params['pax_adult'])) $params['pax_adult'] = '2';
	if(empty($params['pax_child'])) $params['pax_child'] = '0';
	if(empty($params['pax_infant'])) $params['pax_infant'] = '0';
	
	$t = chr(9);
	$cr = chr(13);
	$lf = chr(10);
	
	switch ($params['nights']){
		case "2"; case "3"; case "4"; case "5";
			$min_nights = '2';
			$max_nights = '5';
		break;
		case "6"; case "7"; case "8";
			$min_nights = '6';
			$max_nights = '8';
		break;
		case "9"; case "10"; case "11"; case "12";
			$min_nights = '9';
			$max_nights = '12';
		break;
		case "13"; case "14"; case "15"; case "16"; case "17"; case "18"; case "19"; case "20"; case "21";
			$min_nights = '13';
			$max_nights = '28';
		break;
		default; 
			$min_nights = '';
			$max_nights = '';
		break;
	}


	$request = "HDR".$t."CHO".$t."02".$t."000001".$t.$t.MICROS_API_USER.$t.MICROS_API_PASS.$t."".$t."T".$t."Y".$t."N".$t."C".$cr.$lf;

	$request .= "JNY".$t.$t.$t."250".$t.$params['pax_adult'].$t.$params['pax_child'].$t.$params['pax_infant'].$t.$params['departure_date'].$t."0".$t.$min_nights.$t.$max_nights.$t.$t.$params['destination_airport'].$t.$params['departure_airport'].$t.$t.$t.$t.$t.$t.$t.$cr.$lf;

	$request .= "$$$".$cr.$lf;

	return $request;

}




// FUNCTION // Create search string for all tour operattors
function create_gto_query(){

	$t = chr(9);
	$cr = chr(13);
	$lf = chr(10);

	
	$request = "HDR".$t."GTO".$t."01".$t."000001".$t."".$t."greyapi".$t."52366".$t."".$t."T".$t."Y".$t."Y".$cr.$lf;
	$request .= "$$$".$cr.$lf;

	return $request;


}









// FUNCTION // Get the currency based on the departure airport
function display_price($params){

	// Check airport and convert price to euro
	$euro_price = $this->get_euro_price($params['price'], $params['departure_airport']);
	
	// Add suppliment (in euro) to euro price
	$full_euro_price = ($euro_price + $this->get_suppliment_price($params['tour_op']));

	
	// Convert final price to either Euro or Sterling
	switch ($params['currency']){
		default; case "euro";
			if($params['symbol']){
				if($params['char']){
					$display_price = '�'.floor($full_euro_price);
				}else{
					$display_price = '&euro;'.floor($full_euro_price);
				}
			}else{
				$display_price = floor($full_euro_price);
			}
		break;
		case "sterling";
			$exchange_rates = new exchange_rates();
			$ex_rate = $exchange_rates->get_exchange_rate('GBP',true);
			if($params['symbol']){
				if($params['char']){
					$display_price = '�'.floor($full_euro_price * $ex_rate);
				}else{
					$display_price = '&pound;'.floor($full_euro_price * $ex_rate);
				}
			}else{
				$display_price = floor($full_euro_price * $ex_rate);
			}
		break;
	}

	return $display_price;

}





// FUNCTION // Get the currency based on the departure airport
function get_euro_price($price, $departure_airport=''){

	$airport_code = trim($departure_airport);
	$price = trim($price);
	
	$exchange_rates = new exchange_rates();
	$ex_rate = $exchange_rates->get_exchange_rate('GBP',true);
	
	
	if(in_array($departure_airport, $this->south)){
		$euro_price = $price;
	}elseif(in_array($departure_airport, $this->north)){
	//echo $price.$ex_rate;
		$euro_price = floor($price / $ex_rate);
		//echo $euro_price;
	}else{
		$euro_price = $price;
	}

	return $euro_price;

}





// Function // Get board basis description
function board_basis($board_basis_code){

	switch($board_basis_code){
		case "2S";
			$board_basis = "1-2 Star";
		break;
		case "4S";
			$board_basis = "3-4 Star";
		break;
		case "AB";
			$board_basis = "As Brochured";
		break;
		case "AI";
			$board_basis = "All Inclusive";
		break;
		case "AN";
			$board_basis = "As Notified";
		break;
		case "BB";
			$board_basis = "Bed &amp; Breakfast";
		break;
		case "FB";
			$board_basis = "Full Board";
		break;
		case "FI";
			$board_basis = "Fully Inclusive";
		break;
		case "FO";
			$board_basis = "Flight only";
		break;
		case "HB";
			$board_basis = "Half Board";
		break;
		case "RO";
			$board_basis = "Room Only";
		break;
		case "SC";
			$board_basis = "Self Catering";
		break;
		case "VM";
			$board_basis = "Variable Meals";
		break;
	} 	
		
		
	return $board_basis;
	 	

}



// Function // Get tour operator name
function get_tour_operator($params){

	$params = in($params);
	
	if(!empty($params)){
	
	
		$sql = "SELECT tour_op_code, description 
		FROM tour_operators 
		WHERE tour_op_code = '".$params['tour_op_code']."'
		LIMIT 1 ";
		
		$result = mysql_query($sql) or die (mysql_error());
		
		$row = mysql_fetch_array($result);
		
		if(!$row['description']){
			$row['description'] = 'N/A';
		}
	
		return $row['description'];
		
	}

}


// Function // Get tour operator name
function update_tour_operator_codes(){


	$request = $this->create_gto_query();
	$response = array();
	$response = $this->connection_sabs($request);
	$params=array();
	
	foreach($response as $row){
		$params['tour_op_code']=$row[1];
		$params['description']=$row[14];
		$this->insert_tour_operator_code($params);
	}
	
}


function insert_tour_operator_code($params){

	$params = in($params);
	
	$sql = "REPLACE INTO tour_operators
	SET tour_op_code = '".$params['tour_op_code']."', 
	description = '".$params['description']."' ";
	
	$result = mysql_query($sql) or die (mysql_error());
		
}




// Return true if airport is a ski airport and the departure date is summer
function ski_destination_airport_filter($destination_airport, $date_depart){
	
	$month = round(substr($date_depart, 2, 2));

	if($month > 4 or $month < 11) {
		if(in_array($destination_airport, $this->ski_airports_array))	return true;
	}else{
		return false;	
	}

	
}



}
?>